/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.bean;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.event.ItemSelectEvent;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;
import vehicle.bol.BrandBol;
import vehicle.entity.Brands;

/**
 *
 * @author khoangpv
 */
@ManagedBean
@SessionScoped
public class ChartBean {

    private CartesianChartModel categoryModel;
    private PieChartModel pieModel;

    /** Creates a new instance of ChartBean */
    public ChartBean() {
        createCategoryModel();
        createPieModel();
    }

    public void itemSelect(ItemSelectEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Item selected",
                "Item Index: " + event.getItemIndex() + ", Series Index:" + event.getSeriesIndex());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    private void createCategoryModel() {
        categoryModel = new CartesianChartModel();
        List<Brands> listBran = new ArrayList<Brands>();
        BrandBol bb = new BrandBol();
        listBran = bb.laySoLuongTheoThang();

        for (int i = 0; i < listBran.size(); i++) {
            ChartSeries boys = new ChartSeries();
            boys.setLabel(listBran.get(i).getBrandName());

            boys.set("6 Thang dau nam", listBran.get(i).getBrandID());
            boys.set("6 Thang cuoi nam", listBran.get(i).getTotalCount());
            categoryModel.addSeries(boys);
        }
    }

    private void createPieModel() {
        pieModel = new PieChartModel();

        List<Brands> listBran = new ArrayList<Brands>();
        BrandBol bb = new BrandBol();
        listBran = bb.layPhanTramBanDuoc();

        for (int i = 0; i < listBran.size(); i++) {
            pieModel.set(listBran.get(i).getBrandName(), listBran.get(i).getPercen());
        }
    }
}
